package com.itheima.service.db.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.BlackList;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.mapper.BlackListMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.BlackListService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class BlackListServiceImpl implements BlackListService {
    @Autowired
    private BlackListMapper blackListMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, Long userId) {
        System.out.println("进入sercice");
        IPage<BlackList> page = new Page(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        page = blackListMapper.selectPage(page, queryWrapper);
        List<BlackList> records = page.getRecords();
        ArrayList list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(records)) {
            for (BlackList balck : records) {
                Long balckId = balck.getId();
                UserInfo userInfo = userInfoMapper.selectById(balckId);
                System.out.println(userInfo);
                list.add(userInfo);
            }
        }

        return new PageBeanVo(pageNum,pageSize,page.getTotal(),list);
    }

    @Override
    public void delete(Long userId, Long blackUserId) {
        System.out.println("进入black delete service");
        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("black_user_id",blackUserId);
       blackListMapper.delete(queryWrapper);
    }
}
